/*
 * GWT-Ext Widget Library
 * Copyright 2007 - 2008, GWT-Ext LLC., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
 
package com.hod.client.client;

import com.google.gwt.gears.client.database.DatabaseException;
import com.google.gwt.user.client.ui.CheckBox;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.BooleanFieldDef;
import com.gwtext.client.data.DateFieldDef;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.FloatFieldDef;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.form.DateField;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.grid.BaseColumnConfig;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.menu.BaseItem;
import com.gwtext.client.widgets.menu.Item;
import com.gwtext.client.widgets.menu.event.BaseItemListenerAdapter;
import com.gwtextux.client.data.PagingMemoryProxy;
import com.hod.client.HOD;
import com.hod.client.backend.orm.client.GearsDataStore;
import com.hod.client.backend.orm.client.GearsQuery;
import com.hod.client.frontend.AbstractGrid;
import com.hod.client.lodging.LodgingCRUD;
import com.hod.client.lodging.LodgingRecordset;
import com.hod.client.reserves.ReservationCRUD;
import com.hod.client.room.RoomCRUD;
import com.hod.client.room.RoomRecordset;

public class ClientGrid extends AbstractGrid {
	
	
	private static RecordDef recordDef = new RecordDef(
            new FieldDef[]{
														 
            		new IntegerFieldDef("idClient"), 					 
            		new StringFieldDef("name"), 																		 
            		new StringFieldDef("surname"), 																		 
            		new StringFieldDef("address"), 																		 
            		new StringFieldDef("city"), 																		 
            		new StringFieldDef("country"), 																		 
            		new StringFieldDef("gender"), 																		 
            		new StringFieldDef("ccNumber"), 																		 
            		new StringFieldDef("ccCode"), 																		 
            		new StringFieldDef("howKnowsUs"), 																		 
            		new DateFieldDef("bornDate"), 																		 
            		new StringFieldDef("idNumber"), 																		 
            		new StringFieldDef("kindOfId"), 																		 
            		new StringFieldDef("nationality"), 																											 
            		new BooleanFieldDef("active"), 									 
            		new StringFieldDef("email"), 	
            		new DateFieldDef("checkIn"),
            		new DateFieldDef("checkOut"),
            		new BooleanFieldDef("toalla"), 
            		new FloatFieldDef("total"),
            		new BooleanFieldDef("pago"),
            		new FloatFieldDef("voucher"),
            		new BooleanFieldDef("debe"),
            		new DateFieldDef ("reservationDate"),
            		new IntegerFieldDef ("numberOfNights")
         }
    );

	

	public ClientGrid() {      
    }
	    
	

    public ClientGrid(boolean small,GearsDataStore ds) { 
    	this.ds = ds;    	
    	columns = new BaseColumnConfig[]{
                //new ColumnConfig("idClient","idClient",0, true, null,"idClient"),
                new ColumnConfig("name","name",0, true, null,"name"),
                new ColumnConfig("surname","surname",0, true, null,"surname"),
                //new ColumnConfig("address","address",0, true, null,"address"),
                //new ColumnConfig("city","city",0, true, null,"city"),
                //new ColumnConfig("country","country",0, true, null,"country"),
                //new ColumnConfig("gender","gender",0, true, null,"gender"),
                //new ColumnConfig("ccNumber","ccNumber",0, true, null,"ccNumber"),
                //new ColumnConfig("ccCode","ccCode",0, true, null,"ccCode"),
                //new ColumnConfig("howKnowsUs","howKnowsUs",0, true, null,"howKnowsUs"),
                //new ColumnConfig("bornDate","bornDate",0, true, null,"bornDate"),
                new ColumnConfig("idNumber","idNumber",0, true, null,"idNumber"),
                //new ColumnConfig("kindOfId","kindOfId",0, true, null,"kindOfId"),
                //new ColumnConfig("nationality","nationality",0, true, null,"nationality"),
                //new ColumnConfig("active","active",0, true, null,"active"),
                //new ColumnConfig("email","email",0, true, null,"email"),
        };
    	getData();

        columnModel = getColumnConfigs();
        setColumnModel(columnModel);

        setFrame(true);
        setStripeRows(true);
        setIconCls("grid-icon");
        createPopUp();
        createPager(); 
       
    }
    protected void createGridMenu() {
    	super.createGridMenu();
    	/*gridMenu.addItem(new Item("Reservation",new BaseItemListenerAdapter() { 
	           public void onClick(BaseItem item, EventObject e) { 
	        	   Window reserv = new Window();
	        	   ReservationCRUD resCRUD = new ReservationCRUD();
	        	   reserv.add(resCRUD.createFormPanel());
	        	   resCRUD.setIdClient(getSelectionModel().getSelected().getAsInteger("idClient"));
	        	   reserv.show();
	        	   
	        	} 
	         }));
    	gridMenu.addItem(new Item("Check-in",new BaseItemListenerAdapter() { 
	           public void onClick(BaseItem item, EventObject e) {	        	   
	        	   if (!LodgingRecordset.existsLodgingForClient(getSelectionModel().getSelected().getAsString("idClient"))){
	        		   Window checkin = new Window();
	        		   LodgingCRUD lodCRUD = new LodgingCRUD();
		        	   lodCRUD.setRooms(RoomRecordset.getRoomsAvailable()); //habitaciones disponibles
		        	   lodCRUD.setIdClient(getSelectionModel().getSelected().getAsInteger("idClient"));
		        	   checkin.add(lodCRUD.createFormPanel());
		        	   //HODScreenManager.lodgings.setIdClient(getSelectionModel().getSelected().getAsInteger("idClient"));
		        	   //checkin.add(HODScreenManager.lodgings.createFormPanel());
		        	   checkin.show();	        		   
	        	   }
	        	} 
	         }));
    	gridMenu.addItem(new Item("Check-out",new BaseItemListenerAdapter() { 
	           public void onClick(BaseItem item, EventObject e) {	        	   
	        	   LodgingRecordset.checkOut(String.valueOf(getSelectionModel().getSelected().getAsInteger("idClient")));
	        	} 
	         }));   */ 	
    }
    

	protected void getData(){
    	PagingMemoryProxy proxy = null;
		try {
			GearsQuery q = this.ds.getNamedQuery(Client.FIND_ALL);
			Client[] data = (Client[]) q.execute();
			Client rs = null;
			//if (data.length==0) HOD.initValuesHOD(); //si no tengo valores dentro de la base de datos la inicializo con los valores de prueba
			Object[][]data2 = new Object[data.length][];
			for (int i = 0; i < data.length; i++) {
                rs = data[i];	               
                data2[i] =     new Object[]{
                        rs.getIdClient(),
                        rs.getName(),
                        rs.getSurname(),
                        rs.getAddress(),
                        rs.getCity(),
                        rs.getCountry(),
                        rs.getGender(),
                        rs.getCcNumber(),
                        rs.getCcCode(),
                        rs.getHowKnowsUs(),
                        rs.getBornDate(),
                        rs.getIdNumber(),
                        rs.getKindOfId(),
                        rs.getNationality(),
                        rs.getActive(),
                        rs.getEmail(),
                        rs.getCheckIn(),
                		rs.getCheckOut(),
                		rs.getToalla(), 
                		rs.getTotal(),
                		rs.getPago(),
                		rs.getVoucher(),
                		rs.getDebe(),
                		rs.getReservationDate(),
                		rs.getNumberOfNights()
            };
            }
                        
			proxy = new PagingMemoryProxy(data2);
			
			
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			//Window.alert(e.getMessage());
		} 

        ArrayReader reader = new ArrayReader(recordDef);
        store = new Store(proxy, reader);
        store.load(0,numberRecords);
        setStore(store);
    }

	@Override
	protected void getData(String id) {
		// TODO Auto-generated method stub
		
	}
}
